package com.example.demo.controller;

import com.example.demo.domain.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Merlin
 * @Title: UserController
 * @ProjectName 001spring-boot2-demo
 * @Description: TODO
 * @date 2018/6/2516:19
 */
@RestController
public class UserController {

    /**
     * 构造注入比参数注入的好处：
     * 1.不可修改
     * 2.提前初始化
     */
    private final UserRepository userRepository;

    @Autowired
    public UserController(UserRepository userRepository){
        this.userRepository = userRepository;
    }


    @PostMapping("/person/user")
    public User saveUser(@RequestParam String name){
        User user = new User();
        user.setName(name);
        if(userRepository.save(user)){
            System.out.printf("用户对象 %s ,保存成功！\n",user);
        }
        return user;
    }







}
